const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')

const env = process.env.NODE_ENV || 'development'

// env = 'production'
const config = require('../config/database.json')[env]

// if (process.env.DATABASE_URL) {
//   const sequelize = new Sequelize(process.env.DATABASE_URL, config);
// } else {
const sequelize = new Sequelize(
  config.database,
  config.username,
  config.password,
  config
)
// }
const db = {}

fs
  .readdirSync(__dirname)
  .filter((file) => {
    return file.indexOf('.') !== 0 && file !== 'index.js'
  })
  .forEach((file) => {
    const model = sequelize.import(path.join(__dirname, file))
    db[model.name] = model
  })

Object.keys(db).forEach((modelName) => {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db)
  }
})

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db
